#include "stdafx.h"
#include "EditExUI.h"


CEditExUI::CEditExUI()
{
}


CEditExUI::~CEditExUI()
{
}


void CEditExUI::DoEvent(TEventUI& event)
{
	if (!IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND) {
		if (m_pParent != NULL) m_pParent->DoEvent(event);
		else CLabelUI::DoEvent(event);
		return;
	}

	if (event.Type == UIEVENT_SETCURSOR && IsEnabled())
	{
		::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_IBEAM)));
		return;
	}
	if (event.Type == UIEVENT_WINDOWSIZE)
	{
		if (m_pWindow != NULL) m_pManager->SetFocusNeeded(this);
	}
	if (event.Type == UIEVENT_SCROLLWHEEL)
	{
		if (m_pWindow != NULL) return;
	}
	if (event.Type == UIEVENT_SETFOCUS && IsEnabled())
	{
		Invalidate();
	}
	if (event.Type == UIEVENT_KILLFOCUS && IsEnabled())
	{
		Invalidate();
	}
	if (event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN)
	{
		if (IsEnabled()) {
			GetManager()->ReleaseCapture();
			if (IsFocused() && m_pWindow == NULL)
			{
				m_pWindow = new CEditWnd();
				ASSERT(m_pWindow);
				m_pWindow->Init(this);

				if (PtInRect(&m_rcItem, event.ptMouse))
				{
					int nSize = GetWindowTextLength(*m_pWindow);
					if (nSize == 0)
						nSize = 1;

					Edit_SetSel(*m_pWindow, 0, nSize);
				}
			}
			else if (m_pWindow != NULL)
			{
#if 1
				int nSize = GetWindowTextLength(*m_pWindow);
				if (nSize == 0)
					nSize = 1;

				Edit_SetSel(*m_pWindow, 0, nSize);
#else
				POINT pt = event.ptMouse;
				pt.x -= m_rcItem.left + m_rcTextPadding.left;
				pt.y -= m_rcItem.top + m_rcTextPadding.top;
				::SendMessage(*m_pWindow, WM_LBUTTONDOWN, event.wParam, MAKELPARAM(pt.x, pt.y));
#endif
			}
		}
		return;
	}
	if (event.Type == UIEVENT_MOUSEMOVE)
	{
		return;
	}
	if (event.Type == UIEVENT_BUTTONUP)
	{
		return;
	}
	if (event.Type == UIEVENT_CONTEXTMENU)
	{
		return;
	}
	if (event.Type == UIEVENT_MOUSEENTER)
	{
		if (IsEnabled()) {
			m_uButtonState |= UISTATE_HOT;
			Invalidate();
		}
		return;
	}
	if (event.Type == UIEVENT_MOUSELEAVE)
	{
		if (IsEnabled()) {
			m_uButtonState &= ~UISTATE_HOT;
			Invalidate();
		}
		return;
	}
	CLabelUI::DoEvent(event);
}